package com.data.modules.online.config.service.p020a;

import com.data.modules.online.cgform.extend.ExtendJsonKey;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.cgform.util.OnlFormShowType;
import org.apache.commons.lang.StringUtils;
import com.data.modules.online.config.executor.ColumnMeta;
import com.data.modules.online.config.executor.DataBaseConst;
import com.data.modules.online.config.service.DbTableHandleI;

public class DbTableMysqlHandleImpl implements DbTableHandleI {
    @Override
    public String getAddColumnSql(ColumnMeta columnMeta) {
        return " ADD COLUMN " + getAddSqlByColumnMeta(columnMeta) + ";";
    }

    @Override
    public String getReNameFieldName(ColumnMeta columnMeta) {
        return "CHANGE COLUMN " + columnMeta.getOldColumnName() + " " + getChangeSqlByColumnMeta(columnMeta) + " ;";
    }

    @Override
    public String getUpdateColumnSql(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return " MODIFY COLUMN " + getSqlByColumnMetas(cgformcolumnMeta, datacolumnMeta) + ";";
    }

    @Override
    public String getMatchClassTypeByDataType(String dataType, int digits) {
        if (dataType.equalsIgnoreCase(DataBaseConst.f477l)) {
            return DataBaseConst.f474i;
        }
        if (dataType.equalsIgnoreCase("double")) {
            return "double";
        }
        if (dataType.equalsIgnoreCase("int")) {
            return "int";
        }
        if (dataType.equalsIgnoreCase("Date") || dataType.equalsIgnoreCase("Datetime")) {
            return OnlFormShowType.f320d;
        }
        if (dataType.equalsIgnoreCase(DataBaseConst.f470e)) {
            return "bigdecimal";
        }
        if (dataType.equalsIgnoreCase(ExtendJsonKey.f91e)) {
            return ExtendJsonKey.f91e;
        }
        if (dataType.equalsIgnoreCase("blob")) {
            return "blob";
        }
        return "";
    }

    @Override
    public String dropTableSQL(String tableName) {
        return " DROP TABLE IF EXISTS " + tableName + " ;";
    }

    @Override
    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName + ";";
    }

    private String getBasicSqlByColumnMeta(ColumnMeta aVar, ColumnMeta aVar2) {
        String str;
        String str2 = "";
        if (aVar.getColunmType().equalsIgnoreCase(DataBaseConst.f474i)) {
            StringBuilder append = new StringBuilder().append(aVar.getColumnName()).append(" varchar(").append(aVar.getColumnSize()).append(") ");
            if ("Y".equals(aVar.getIsNullable())) {
                str = "NULL";
            } else {
                str = "NOT NULL";
            }
            str2 = append.append(str).toString();
        } else if (aVar.getColunmType().equalsIgnoreCase(OnlFormShowType.f320d)) {
            str2 = aVar.getColumnName() + " datetime " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("int")) {
            str2 = aVar.getColumnName() + " int(" + aVar.getColumnSize() + ") " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("double")) {
            str2 = aVar.getColumnName() + " double(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ") " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("bigdecimal")) {
            str2 = aVar.getColumnName() + " decimal(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ") " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase(ExtendJsonKey.f91e)) {
            str2 = aVar.getColumnName() + " text " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("blob")) {
            str2 = aVar.getColumnName() + " blob " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        }
        String str3 = (str2 + (StringUtils.isNotEmpty(aVar.getComment()) ? " COMMENT '" + aVar.getComment() + CgformUtil.f229C : " ")) + (StringUtils.isNotEmpty(aVar.getFieldDefault()) ? " DEFAULT " + aVar.getFieldDefault() : " ");
        String pkType = aVar.getPkType();
        if (!"id".equalsIgnoreCase(aVar.getColumnName()) || pkType == null) {
            return str3;
        }
        if ("SEQUENCE".equalsIgnoreCase(pkType) || "NATIVE".equalsIgnoreCase(pkType)) {
            return str3 + " AUTO_INCREMENT ";
        }
        return str3;
    }

    private String getSqlByColumnMetas(ColumnMeta aVar, ColumnMeta aVar2) {
        return getBasicSqlByColumnMeta(aVar, aVar2);
    }

    private String getAddSqlByColumnMeta(ColumnMeta aVar) {
        return getBasicSqlByColumnMeta(aVar, null);
    }

    private String getChangeSqlByColumnMeta (ColumnMeta aVar) {
        return getBasicSqlByColumnMeta(aVar, null);
    }

    @Override
    public String getCommentSql(ColumnMeta columnMeta) {
        return "";
    }

    @Override
    public String getSpecialHandle(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return null;
    }

    @Override
    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName + CgformUtil.f293d + tableName;
    }

    @Override
    public String countIndex(String indexName, String tableName) {
        return "select COUNT(*) from information_schema.statistics where table_name = '" + tableName + "'  AND index_name = '" + indexName + CgformUtil.f229C;
    }
}
